package cn.schoolwow.quickdao.module.database.dml.instance.flow.common.filter;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.dml.CheckStrategy;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickdao.module.database.dml.common.flow.CheckFieldConstraintFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instance.QueryInstance;

import java.util.*;

public class FilterInstanceByConstraintFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        shouldCheck(flowContext);
        filterInstanceByConstraint(flowContext);
    }

    @Override
    public String name() {
        return "过滤字段值不符合字段约束的实例";
    }

    private void shouldCheck(FlowContext flowContext){
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);
        CheckStrategy checkStrategy = null!=manipulationOption.checkStrategy?manipulationOption.checkStrategy:quickDAOConfig.databaseOption.checkStrategy;
        if(CheckStrategy.None.equals(checkStrategy)){
            flowContext.brokenCurrentFlow("根据字段约束策略,跳过过滤字段值不符合字段约束的实例");
        }
    }

    private void filterInstanceByConstraint(FlowContext flowContext){
        Entity entity = (Entity) flowContext.checkData("entity");
        Object[] instances = (Object[]) flowContext.checkData("instances");
        Map<Object,String> instanceReasonMap = (Map<Object, String>) flowContext.getData("instanceReasonMap", new LinkedHashMap<String,Object>());

        List<Object> objectList = new ArrayList<>(Arrays.asList(instances));
        Iterator<Object> iterator = objectList.iterator();
        while(iterator.hasNext()){
            Object instance = iterator.next();
            for(Property property:entity.properties){
                Object value = QueryInstance.newQuery(instance)
                        .execute()
                        .getSingleFieldValue(property.name);
                String reason = (String) flowContext.getQuickFlow().startFlow(new CheckFieldConstraintFlow())
                        .putTemporaryData("property", property)
                        .putTemporaryData("value", value)
                        .execute()
                        .getData("reason");
                if(null!=reason){
                    if(!instanceReasonMap.containsKey(instance)){
                        instanceReasonMap.put(instance, reason);
                    }else{
                        String oldReason = instanceReasonMap.get(instance);
                        instanceReasonMap.put(instance, oldReason+";"+reason);
                    }
                }
            }
            if(instanceReasonMap.containsKey(instance)){
                iterator.remove();
            }
        }

        if(objectList.isEmpty()){
            flowContext.putData("effect", 0);
            flowContext.broken("根据字段约束过滤数据后实例列表为空");
        }
        flowContext.putTemporaryData("instances", objectList.toArray(new Object[0]));
    }

}
